/** 所有注释不能使用 // */
(()=>{
    function sleep(timeout) {
        return new Promise(resolve => {
            setTimeout(resolve, timeout);
        });
    }
    /* 签到 */
    async function sign() {
        /* 点击签到按钮 */
        document.querySelector('.control-btn-content-item').click();
        await sleep(1000);
        /* 点击签到 */
        let btn = document.querySelector('#checkInBtn');
        if (btn) {
            btn.click();
            await sleep(1000);
        }
        /* 关闭弹窗 */
        btn = document.querySelector('.checkIn-cancel');
        if (btn) {
            btn.click();
            await sleep(1000);
        }
        btn = document.querySelector('.ThreeMealToast-closeBtn');
        if (btn) {
            btn.click();
            await sleep(1000);
        }
    }
    /* 浇水 */
    async function dropWater() {
        /* 检查是否已经浇水了 */
        document.querySelector('.taro-img.control-btn-content-item-img.task').click();
        await sleep(1000);
        let done = true;
        let btnGroup = document.querySelectorAll('.taro-scroll.taro-scroll-view__scroll-y.signInList-inner-list .signInList-inner-list-item');
        btnGroup.forEach(btn => {
            if (btn.innerHTML.indexOf('每日累计浇水10次') > 0) {
                done = (btn.innerHTML.indexOf('已领取') > 0);
            }
        });
        /* 关闭弹窗 */
        document.querySelector('.SignInList-closeBtn').click();
        await sleep(1000);
        if (done) {
            console.log('已浇过水');
            return;
        }
        /* 开始浇水 */
        let total = 10;
        let count = 0;
        do {
            document.querySelector('.taro-img.control-btn-content-right-drop-bac').click();
            console.log('第 ' + count + ' 次浇水');
            await sleep(3500);
        } while (count++ < total);
    }
    /* 做任务领水滴 */
    async function doTask() {
        document.querySelector('.taro-img.control-btn-content-item-img.task').click();
        await sleep(1000);
        let btns = document.querySelectorAll('.signInList-inner-list-item-right-down-bac');
        let unDoTaskBtns = [];
        btns.forEach(async (btn) => {
            if (btn.innerHTML === '去领取' || btn.innerHTML === '领取') {
                btn.click();
                await sleep(1000);
                let closeBtn = document.querySelector('.ThreeMealToast-closeBtn');
                if (closeBtn) {
                    closeBtn.click();
                    await sleep(1000);
                }
            } else if (btn.innerHTML === '去逛逛' || btn.innerHTML === '去看看') {
                unDoTaskBtns.push(btn);
            }
        });
        if (unDoTaskBtns.length > 0) {
            unDoTaskBtns[0].click();
            await sleep(10000);
        }
        /* 关闭弹窗 */
        document.querySelector('.SignInList-closeBtn').click();
        await sleep(1000);
    }

    /* 领幸运水滴 */
    async function getLuck() {
        let btn = document.querySelector('.easterEggBrowseToast-Receive');
        if (btn) {
            btn.click();
            console.log('领取辛运水滴');
            await sleep(1000);
        }
    }
    /* 执行入口 */
    async function main() {
        getLuck();
        await sign();
        await dropWater();
        await doTask();
    }
    /* 如果是种树地址，则执行任务， 否则停留11秒返回 */
    let treeUrl = "https://h5.m.jd.com/babelDiy/Zeus/3KSjXqQabiTuD1cJ28QskrpWoBKT/index.html";
    let loginUrl = "https://plogin.m.jd.com/login/login";
    let rainUrl = "https://h5.m.jd.com/babelDiy/Zeus/2SHa5TqJiM5sBC4svoLLxG1CAp3a/index.html";
    if (window.location.href.indexOf(treeUrl) == 0) {
        main();
    } else if (window.location.href.indexOf(rainUrl) == 0) {
        /** 如果是水滴雨，停留 秒后返回 */
        console.log('18秒后返回种树页面');
        setTimeout(() => {
            window.location = treeUrl;
        }, 18000);
    } else if (window.location.href.indexOf(loginUrl) != 0) {
        /** 非登录页面，停留11秒后返回种树页面 */
        console.log('11秒后返回种树页面');
        setTimeout(() => {
            window.location = treeUrl;
        }, 11000);
    }
})();